package dao

import (
	"fmt"
	"gin/model"
	"gin/tool"
)

type GoodsDao struct {
	*tool.Orm
}

//var goodsWgExecute sync.WaitGroup
func (dao *GoodsDao) GoodsCateList(page int, cateId string, pageSize int) interface{} {
	goods := make([]model.GoodsJoinCate, 0)
	err := dao.Table("goods").
		Where("goods_cate_id = ?", cateId).
		Limit(pageSize, (page-1)*pageSize).
		Join("INNER", "goods_cate", "goods.goods_cate_id = goods_cate.id").
		//Join("INNER", "image", "goods_cate.image_id = image.id").
		Find(&goods)
	if err != nil {
		fmt.Println("---商品数据查询失败---", err)
		return nil
	}
	//image := model.Image{}
	//_,_= dao.Table("image").Where("id = ?",goods[0].ImageId).Get(&image)
	//fmt.Println(image)
	//fmt.Println(goods[0].)
	/*goodsWgExecute.Add(1)
	go func() {
		for key,value := range goods{
			if value.ImageId == ""{
				continue
			}
			image := model.Image{}
			_,_= dao.Table("image").Where("id = ?",value.ImageId).Get(&image)
			goods[key].CateImageUrl = image.Url
		}
		goodsWgExecute.Done() //子协成执行完成
	}()
	goodsWgExecute.Wait() //等待子协成执行完成*/
	return &goods
}

func (dao *GoodsDao) GoodsTotal(key string, value string) int64 {
	goods := make([]model.Goods, 0)
	total, _ := dao.Table("goods").Where(key+" = ?", value).Count(goods)
	return total
}
